﻿using System.Data;
using Litografia.BusinessEntities;

namespace Litografia.DataObjects
{
    public sealed class DOOperario : DOBase
    {
        #region Declarations

        private const string GetSP = "operarioGet";
        private const string UpdateSP = "operarioUpdate";
        private const string DeleteSP = "operarioDelete";
        private const string AddSP = "operarioAdd";

        #endregion

        #region Methods: Public
        public static BEOperario.OperarioDataTable Get(int empresaId)
        {
            var result = new BEOperario();

            var command = DB.GetStoredProcCommand(GetSP);
            DB.AddInParameter(command, "@empresa_id", DbType.Int32, empresaId);
            var dataset = DB.ExecuteDataSet(command);

            // Pasa informacion hacia el BE
            if (dataset != null && dataset.Tables.Count > 0)
            {
                dataset.Tables[0].TableName = result.Tables[0].TableName;
                result.Merge(dataset);
                result.EnforceConstraints = true;
            }
            return result.Operario;
        }

        public static void Update(BEOperario.OperarioRow operario)
        {
            var command = DB.GetStoredProcCommand(UpdateSP);
            DB.AddInParameter(command, "@id", DbType.Int32, operario.Id);
            DB.AddInParameter(command, "@nombre", DbType.String, operario.Nombre);
            DB.AddInParameter(command, "@puesto_id", DbType.Int32, operario.PuestoId);
            DB.AddInParameter(command, "@costo_hora", DbType.Decimal, operario.CostoxHora);
            DB.ExecuteNonQuery(command);
        }

        public static void Delete(int id)
        {
            var command = DB.GetStoredProcCommand(DeleteSP);
            DB.AddInParameter(command, "@id", DbType.Int32, id);
            DB.ExecuteNonQuery(command);
        }
        
        public static void Add(BEOperario.OperarioRow operario)
        {
            var command = DB.GetStoredProcCommand(AddSP);
            DB.AddInParameter(command, "@empresa_id", DbType.Int32, operario.EmpresaId);
            DB.AddInParameter(command, "@nombre", DbType.String, operario.Nombre);
            DB.AddInParameter(command, "@puesto_id", DbType.Int32, operario.PuestoId);
            DB.AddInParameter(command, "@costo_hora", DbType.Decimal, operario.CostoxHora);
            DB.ExecuteNonQuery(command);
        }

        #endregion
    }
}
